﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace DoNet.Utils
{
    public class CacheHelper
    {
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        public static object GetCache(string CacheKey)
        {
            Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        public static void SetCache(string CacheKey, object obj)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, obj);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="Timeout"></param>
        public static void SetCache(string CacheKey, object obj, TimeSpan Timeout)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, obj, null, DateTime.MaxValue, Timeout, CacheItemPriority.NotRemovable, null);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="expire"></param>
        public static void SetCache(string CacheKey, object obj, int expire)
        {
            Cache objCache = HttpRuntime.Cache;
            //表示永不过期
            if (expire == 0)
            {
                objCache.Insert(CacheKey, obj, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, null);
            }
            else
            {
                objCache.Insert(CacheKey, obj, null, DateTime.Now.AddSeconds(expire), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        public static void SetCache(string CacheKey, object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, obj, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        public static void RemoveCache(string CacheKey)
        {
            Cache _cache = HttpRuntime.Cache;
            _cache.Remove(CacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            Cache _cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }
    }
}
